<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>/home/zjh/git/Koala-UI/KoalaUI/WebContent/lib/Koala_ComboBox.js</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
    <script src="http://yui.yahooapis.com/combo?3.9.1/build/yui/yui-min.js"></script>
</head>
<body class="yui3-skin-sam">

<div id="doc">
    <div id="hd" class="yui3-g header">
        <div class="yui3-u-3-4">
            
                <h1><img src="../assets/css/logo.png" title=""></h1>
            
        </div>
        <div class="yui3-u-1-4 version">
            <em>API Docs for: </em>
        </div>
    </div>
    <div id="bd" class="yui3-g">

        <div class="yui3-u-1-4">
            <div id="docs-sidebar" class="sidebar apidocs">
                <div id="api-list">
    <h2 class="off-left">APIs</h2>
    <div id="api-tabview" class="tabview">
        <ul class="tabs">
            <li><a href="#api-classes">Classes</a></li>
            <li><a href="#api-modules">Modules</a></li>
        </ul>

        <div id="api-tabview-filter">
            <input type="search" id="api-filter" placeholder="Type to filter APIs">
        </div>

        <div id="api-tabview-panel">
            <ul id="api-classes" class="apis classes">
            
                <li><a href="../classes/Accordion.html">Accordion</a></li>
            
                <li><a href="../classes/AutoComplete.html">AutoComplete</a></li>
            
                <li><a href="../classes/Button.html">Button</a></li>
            
                <li><a href="../classes/Carousel.html">Carousel</a></li>
            
                <li><a href="../classes/ColorPicker.html">ColorPicker</a></li>
            
                <li><a href="../classes/Combobox.html">Combobox</a></li>
            
                <li><a href="../classes/DateTimePicker.html">DateTimePicker</a></li>
            
                <li><a href="../classes/Grid.html">Grid</a></li>
            
                <li><a href="../classes/ListView.html">ListView</a></li>
            
                <li><a href="../classes/Popover.html">Popover</a></li>
            
                <li><a href="../classes/ProgressBar.html">ProgressBar</a></li>
            
                <li><a href="../classes/RichTextEditor.html">RichTextEditor</a></li>
            
                <li><a href="../classes/ScrollSpy.html">ScrollSpy</a></li>
            
                <li><a href="../classes/Select.html">Select</a></li>
            
                <li><a href="../classes/SideBar.html">SideBar</a></li>
            
                <li><a href="../classes/Slider.html">Slider</a></li>
            
                <li><a href="../classes/Spinner.html">Spinner</a></li>
            
                <li><a href="../classes/Tab.html">Tab</a></li>
            
                <li><a href="../classes/ToolBar.html">ToolBar</a></li>
            
                <li><a href="../classes/Tooltip.html">Tooltip</a></li>
            
                <li><a href="../classes/Tree.html">Tree</a></li>
            
                <li><a href="../classes/Wizard.html">Wizard</a></li>
            
            </ul>

            <ul id="api-modules" class="apis modules">
            
            </ul>
        </div>
    </div>
</div>

            </div>
        </div>
        <div class="yui3-u-3-4">
                <div id="api-options">
        Show:
        <label for="api-show-inherited">
            <input type="checkbox" id="api-show-inherited" checked>
            Inherited
        </label>

        <label for="api-show-protected">
            <input type="checkbox" id="api-show-protected">
            Protected
        </label>

        <label for="api-show-private">
            <input type="checkbox" id="api-show-private">
            Private
        </label>
        <label for="api-show-deprecated">
            <input type="checkbox" id="api-show-deprecated">
            Deprecated
        </label>

    </div>


            <div class="apidocs">
                <div id="docs-main">
                    <div class="content">
                        <h1 class="file-heading">File: /home/zjh/git/Koala-UI/KoalaUI/WebContent/lib/Koala_ComboBox.js</h1>

<div class="file">
    <pre class="code prettyprint linenums">
/**
 * @author zjh
 */
+function ($) {
	&quot;use strict&quot;;
	/**
	 * 下拉组合框
	 * @class Combobox
	 * @constructor
	 * @example
	 * 		js代码
	 *		$(function(){
	 *			$(&#x27;#combobox-selectByValue&#x27;).on(&#x27;click&#x27;, function () {
	 *				$(&#x27;#MyCombobox1&#x27;).combobox(&#x27;selectByValue&#x27;, &#x27;3&#x27;);
	 *			});
	 *		});
	 * 
	 * 		html代码
	 * 		&lt;div class=&quot;container&quot;&gt;	
	 * 			 &lt;div class=&quot;col-lg-2&quot;&gt;
	 *			    &lt;div id=&quot;MyCombobox1&quot; class=&quot;input-group combobox&quot;&gt;
	 *			      &lt;input type=&quot;text&quot; class=&quot;form-control&quot;&gt;
	 *			      &lt;div class=&quot;input-group-btn&quot;&gt;
	 *			        &lt;button type=&quot;button&quot; class=&quot;btn btn-default dropdown-toggle&quot; data-toggle=&quot;dropdown&quot;&gt;
	 *			        	&lt;span class=&quot;caret&quot;&gt;&lt;/span&gt;
	 *			        &lt;/button&gt;
	 *			        &lt;ul class=&quot;dropdown-menu pull-right&quot;&gt;
	 *			          &lt;li data-value=&quot;1&quot;&gt;&lt;a href=&quot;#&quot;&gt;广州&lt;/a&gt;&lt;/li&gt;
	 *			          &lt;li data-value=&quot;2&quot;&gt;&lt;a href=&quot;#&quot;&gt;深圳&lt;/a&gt;&lt;/li&gt;
	 *			          &lt;li data-fizz=&quot;buzz&quot; data-value=&quot;3&quot;&gt;&lt;a href=&quot;#&quot;&gt;北京&lt;/a&gt;&lt;/li&gt;
	 *			          &lt;li class=&quot;divider&quot;&gt;&lt;/li&gt;
	 *			          &lt;li data-value=&quot;4&quot;&gt;&lt;a href=&quot;#&quot;&gt;国外&lt;/a&gt;&lt;/li&gt;
	 *			        &lt;/ul&gt;
	 *			      &lt;/div&gt;
	 *			    &lt;/div&gt;
	 *			  &lt;/div&gt;
	 *			  &lt;input type=&quot;button&quot; value=&quot;select by value&quot; id=&quot;combobox-selectByValue&quot; class=&quot;btn btn-default&quot;&gt;
	 *		&lt;/div&gt;
	 */
	var Combobox = function (element, options) {
		this.$element = $(element);
		this.options = $.extend({}, $.fn.combobox.defaults, options);
		this.$element.on(&#x27;click&#x27;, &#x27;a&#x27;, $.proxy(this.itemclicked, this));
		this.$element.on(&#x27;change&#x27;, &#x27;input&#x27;, $.proxy(this.inputchanged, this));
		this.$input = this.$element.find(&#x27;input&#x27;);
		this.$button = this.$element.find(&#x27;.btn&#x27;);

		// set default selection
		this.setDefaultSelection();
	};

	Combobox.prototype = {
		
		constructor: Combobox,
		
		/**
		 * 获取选择的值，并生成js对象
		 * @method selectedItem
		 * @returns {text:文本值}
		 */
		selectedItem: function () {
			var item = this.$selectedItem;
			var data = {};

			if (item) {
				var txt = this.$selectedItem.text();
				data = $.extend({ text: txt }, this.$selectedItem.data());
			}
			else {
				data = { text: this.$input.val()};
			}

			return data;
		},

		/**
		 * 根据内容选择
		 * @method selectByText
		 * @param text
		 */
		selectByText: function (text) {
			var selector = &#x27;li:fuelTextExactCI(&#x27; + text + &#x27;)&#x27;;
			this.selectBySelector(selector);
		},

		/**
		 * 根据data-value属性值选择
		 * @method selectByValue
		 * @param value
		 */
		selectByValue: function (value) {
			var selector = &#x27;li[data-value=&quot;&#x27; + value + &#x27;&quot;]&#x27;;
			this.selectBySelector(selector);
		},

		/**
		 * 根据li索引选择
		 * @method selectByIndex
		 * @param index
		 */
		selectByIndex: function (index) {
			// zero-based index
			var selector = &#x27;li:eq(&#x27; + index + &#x27;)&#x27;;
			this.selectBySelector(selector);
		},

		/**
		 * 根据jQuery选择器选中
		 * @method selectBySelector
		 * @param selector
		 */
		selectBySelector: function (selector) {
			var $item = this.$element.find(selector);

			if (typeof $item[0] !== &#x27;undefined&#x27;) {
				this.$selectedItem = $item;
				this.$input.val(this.$selectedItem.text());
			}
			else {
				this.$selectedItem = null;
			}
		},

		/**
		 * 设置第一条选中
		 * @method setDefaultSelection
		 */
		setDefaultSelection: function () {
			var selector = &#x27;li[data-selected=true]:first&#x27;;
			var item = this.$element.find(selector);

			if (item.length &gt; 0) {
				// select by data-attribute
				this.selectBySelector(selector);
				item.removeData(&#x27;selected&#x27;);
				item.removeAttr(&#x27;data-selected&#x27;);
			}
		},

		/**
		 * 设置输入框可用
		 * @method enable
		 */
		enable: function () {
			this.$input.removeAttr(&#x27;disabled&#x27;);
			this.$button.removeClass(&#x27;disabled&#x27;);
		},

		/**
		 * 设置输入框不可用
		 * @method disable
		 */
		disable: function () {
			this.$input.attr(&#x27;disabled&#x27;, true);
			this.$button.addClass(&#x27;disabled&#x27;);
		},

		itemclicked: function (e) {
			this.$selectedItem = $(e.target).parent();

			// set input text and trigger input change event marked as synthetic
			this.$input.val(this.$selectedItem.text()).trigger(&#x27;change&#x27;, { synthetic: true });

			// pass object including text and any data-attributes
			// to onchange event
			var data = this.selectedItem();

			// trigger changed event
			this.$element.trigger(&#x27;changed&#x27;, data);

			e.preventDefault();
		},

		inputchanged: function (e, extra) {

			// skip processing for internally-generated synthetic event
			// to avoid double processing
			if (extra &amp;&amp; extra.synthetic) return;

			var val = $(e.target).val();
			this.selectByText(val);

			// find match based on input
			// if no match, pass the input value
			var data = this.selectedItem();
			if (data.text.length === 0) {
				data = { text: val };
			}

			// trigger changed event
			this.$element.trigger(&#x27;changed&#x27;, data);

		}

	};


	// COMBOBOX PLUGIN DEFINITION

	$.fn.combobox = function (option, value) {
		var methodReturn;

		var $set = this.each(function () {
			var $this = $(this);
			var data = $this.data(&#x27;koala.combobox&#x27;);
			var options = typeof option === &#x27;object&#x27; &amp;&amp; option;

			if (!data) $this.data(&#x27;koala.combobox&#x27;, (data = new Combobox(this, options)));
			if (typeof option === &#x27;string&#x27;) methodReturn = data[option](value);
		});

		return (methodReturn === undefined) ? $set : methodReturn;
	};

	//默认属性列表
	$.fn.combobox.defaults = {
		//以下新增属性暂时不实现，下期再来升级
		mutiSelect:false,				//支持多选
		dataGrid:null,					//使用哪个表格，如果是null，就表示不用
		ajaxUrl:null					//异步加载URL，如果有，则不用data
	};

	$.fn.combobox.Constructor = Combobox;


	// COMBOBOX DATA-API

	$(function () {

		$(window).on(&#x27;load&#x27;, function () {
			$(&#x27;.combobox&#x27;).each(function () {
				var $this = $(this);
				if ($this.data(&#x27;koala.combobox&#x27;)) return;
				$this.combobox($this.data());
			});
		});

		$(&#x27;body&#x27;).on(&#x27;mousedown.combobox.data-api&#x27;, &#x27;.combobox&#x27;, function (e) {
			var $this = $(this);
			if ($this.data(&#x27;koala.combobox&#x27;)) return;
			$this.combobox($this.data());
		});
	});

}(window.jQuery);
    </pre>
</div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="../assets/js/yui-prettify.js"></script>
<script src="../assets/../api.js"></script>
<script src="../assets/js/api-filter.js"></script>
<script src="../assets/js/api-list.js"></script>
<script src="../assets/js/api-search.js"></script>
<script src="../assets/js/apidocs.js"></script>
</body>
</html>
